{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# NumPy 切片和索引\n",
    "# ndarray 数组可以基于 0 - n 的下标进行索引，\n",
    "# [1] 切片对象可以通过内置的 slice 函数，并设置 start, stop 及 step 参数进行，从原数组中切割出一个新数组\n",
    "# [2] 可以通过冒号分隔切片参数 start:stop:step 来进行切片操作\n",
    "# [3] 切片还可以包括省略号 …，来使选择元组的长度与数组的维度相同。 如果在行位置使用省略号，它将返回包含行中元素的 ndarray。\n",
    "# \n",
    "import numpy as np\n",
    "\n",
    "# [1] slice 函数\n",
    "print(\"[1] slice 函数\")\n",
    "a = np.arange(10)\n",
    "s = slice(2, 7, 2)   # 从索引 2 开始到索引 7 停止，步长为2\n",
    "print(a)\n",
    "print(a[s])\n",
    "print()\n",
    "\n",
    "# [2] 切片参数 start:stop:step\n",
    "print(\"[2] 切片参数 start:stop:step\")\n",
    "a = np.arange(10)\n",
    "print(\"a = \", a)\n",
    "print(\"a[2:] = \", a[2:])\n",
    "print(\"a[2:7] = \", a[2:7])\n",
    "print(\"a[2:7:2] = \", a[2:7:2])\n",
    "print()\n",
    "\n",
    "# 多维数组\n",
    "a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])\n",
    "print(a)\n",
    "# 从某个索引处开始切割\n",
    "print('a[1:] = ', a[1:])\n",
    "print()\n",
    "\n",
    "# [3] 省略号 ...\n",
    "a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])\n",
    "print(a)   # 第2列元素\n",
    "print(a[..., 1])   # 第2列元素\n",
    "print(a[1, ...])   # 第2行元素\n",
    "print(a[..., 1:])  # 第2列及剩下的所有元素\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "cf18841ace8313d0bc088ca146c17a6c0040e82121d5cb75c0ea07172309253d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
